//
//  ActListEViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/10/6.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "ActListEViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"
#import "ActCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "huiyuanDetailsViewController.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "shouyingViewController.h"
#import "BusinessActDetailViewController.h"
@interface ActListEViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;


@end

@implementation ActListEViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*couponId;
    NSMutableArray*title;
    NSMutableArray*price;
    NSMutableArray*qty;
    NSMutableArray*beginDate;
    NSMutableArray*endDate;
    NSMutableArray*imageUrl;
    UILabel* noLabel;
    NSMutableArray*imageUrlFull;
    NSString *result;
    int pageNumber;
    MBProgressHUD *progressHud;
    
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
   
    
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [self addFooter];
    _table.delegate=self;
    _table.dataSource=self;
    [self.view addSubview:_table];
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    commdity=[HAODEsingleton shared];
     [self add];
    
}

-(void)addProduct:(UIButton*)sender{
    
    GetProductListViewController *detailViewController = [[GetProductListViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
    
}


-(void)add{
    
    [noLabel removeFromSuperview];
    
    self.sat=nil;
    
    
    pageNumber = 1;
    
    
    
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue: @"E" forKey:@"dataType"];
    [param setValue: @"20" forKey:@"pageSize"];
    [param setValue: @"1" forKey:@"pageIndex"];
    
    NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    
    
    [HttpRequest requestData:@"Business/Act/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSString*TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                
                if (TotalCount.intValue==0) {
                    
                   noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 17)];
                    noLabel.text=@"无数据";
                    noLabel.textColor=[UIColor blackColor];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [self.view addSubview:noLabel];
                    
                }else{
                    
                    imageUrlFull= [[NSMutableArray alloc] init];
                    imageUrl= [[NSMutableArray alloc] init];
                    endDate= [[NSMutableArray alloc] init];
                    beginDate= [[NSMutableArray alloc] init];
                    qty= [[NSMutableArray alloc] init];
                    price= [[NSMutableArray alloc] init];
                    title= [[NSMutableArray alloc] init];
                    couponId = [[NSMutableArray alloc] init];

                    
                    
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arrr )
                    {
                        [title addObject:[dicc valueForKey:@"actName"]];
//                        [price addObject:[dicc valueForKey:@"price"]];
//                        [qty addObject:[dicc valueForKey:@"qty"]];
                        [couponId addObject:[dicc valueForKey:@"actID"]];
                        [beginDate addObject:[dicc valueForKey:@"beginDate"]];
                        [endDate addObject:[dicc valueForKey:@"endDate"]];
                        [imageUrlFull addObject:[dicc valueForKey:@"imageUrlFull"]];
                        
                        
                    }
                    
                    [_table reloadData];
                    
                }
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    //                   NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //                  [Global showTextHud:requestResultStr view:self.view];
                    
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
    
    
    
}





//上拉刷新
- (void)addFooter
{
    
    
    
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
    
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [_table reloadData];
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [title count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    ActCell* cell = (ActCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ActCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    
    //cell.accessoryView = jiantou;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView*bianxian=[[UIView alloc]initWithFrame:CGRectMake(8, 8, ScreenWidth-16,200-8)];
    
    [bianxian.layer setCornerRadius:0.5];
    [bianxian.layer setBorderWidth:0.5];
    [bianxian.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){204.0/255.0, 204.0/255.0, 204.0/255.0, 1.0  });
    [bianxian.layer setBorderColor:colorref2];//边框颜色
    [cell.contentView addSubview:bianxian];
    
    
    
    [cell.imageUrlFull.layer setBorderColor:colorref2];//边框颜色
    [cell.imageUrlFull.layer setCornerRadius:0.5];
    [cell.imageUrlFull.layer setBorderWidth:0.1];
    [cell.imageUrlFull.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace21 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref21 = CGColorCreate(colorSpace21,(CGFloat[]){ 0, 0, 0, 0.3 });
    [cell.imageUrlFull.layer setBorderColor:colorref21];//边框颜色
    [cell.imageUrlFull setImageWithURL:[NSURL URLWithString:[imageUrlFull objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@""]];
     [cell.imageUrlFull setContentMode:UIViewContentModeScaleToFill];
    
    
    cell.actName.text =[NSString stringWithFormat:@"%@",  [title objectAtIndex:indexPath.row]];
    cell.actName.font=[UIFont systemFontOfSize:15];
    cell.actName.textColor=zhonghuiColor;
    NSRange range={0,10};
    NSString* string =[[endDate objectAtIndex:indexPath.row] substringWithRange:range];

    cell.sat.text =[NSString stringWithFormat:@"%@ 到期",  string];
    cell.sat.textColor=zhonghuiColor;
//    cell.price.text =[NSString stringWithFormat:@"¥ %@",  [price objectAtIndex:indexPath.row]];
    
    
    UIView* huimen=[[UIView alloc]init];
    huimen.frame=CGRectMake(0, 0, ScreenWidth, cell.height);
    huimen.backgroundColor=[UIColor whiteColor];
    huimen.alpha=0.2;
    [cell.contentView addSubview:huimen];
    
    
    
    
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  200;
}

#pragma mark - Table view delegate

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    commdity.orderId=couponId[indexPath.row];
    
    BusinessActDetailViewController*vc1=[[BusinessActDetailViewController alloc] init];
    
    [self.navigationController pushViewController:vc1 animated:YES];
}



- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}


@end
